#include "stdafx.h"
#include "ThreadsInfo.h"
#include <TlHelp32.h>
#include <Psapi.h>
#include <process.h>
#include <stdio.h>

using namespace std;

CThreadsInfo::CThreadsInfo()
{
	
}


CThreadsInfo::~CThreadsInfo()
{
}

int CThreadsInfo::getThreadNumber()
{
	int iThreadNumber = 0;
	PROCESSENTRY32 pe32 = { 0 };
	pe32.dwSize = sizeof(pe32);
	int pid = _getpid();


	HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if (hProcessSnap == INVALID_HANDLE_VALUE) {
		return -1;
	}
	BOOL bMore = ::Process32First(hProcessSnap, &pe32);
	while (bMore)
	{
		if (pe32.th32ProcessID == pid) {
			break;
		}

		bMore = ::Process32Next(hProcessSnap, &pe32);
	}
	

	iThreadNumber = pe32.cntThreads;
	return iThreadNumber;
}

string CThreadsInfo::getInfo()
{
	HANDLE hProcess = ::GetCurrentProcess();
	PROCESS_MEMORY_COUNTERS pmc;
	GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc));


	int iThreadCount = getThreadNumber();

	char szBuffer[256];
	sprintf_s(szBuffer,"Memory: %d k \tThreadCount: %d ", pmc.WorkingSetSize / 1000,iThreadCount);
	return szBuffer;

}
